package com.taotao.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import com.alibaba.druid.sql.PagerUtils;
import com.alibaba.dubbo.remoting.Client;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.pojo.EasyUIDataGridResult;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.util.IDUtils;
import com.taotao.common.util.JsonUtils;
import com.taotao.manager.jedis.JedisClient;
import com.taotao.mapper.TbItemDescMapper;
import com.taotao.mapper.TbItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemDesc;
import com.taotao.pojo.TbItemExample;
import com.taotao.pojo.TbItemExample.Criteria;
import com.taotao.service.ItemService;

@Service
public class ItemServiceImpl implements ItemService {

	@Autowired
	private TbItemMapper mapper;
	
	@Autowired
	private TbItemDescMapper itemDescMapper;
	
	@Autowired
	private JedisClient client;
	
	
	@Autowired
	private JmsTemplate jmsTemplate;
	
	@Resource(name="topicDestination")
	private Destination destination;
	
	@Value("${ITEM_INFO_KEY}")
	private String ITEM_INFO_KEY;
	
	@Value("${ITEM_INFO_KEY_EXPIRE}")
	private Integer ITEM_INFO_KEY_EXPIRE;
	
	@Override
	public EasyUIDataGridResult getItemList(Integer page, Integer rows) {
		//1.设置分页信息
		if(page == null) page =1;
		if(rows == null) page =1;
		PageHelper.startPage(page, rows);
		//2.注入mapper
		//3.创建example对象，。不需要设置对象
		TbItemExample example = new TbItemExample(); 
		//根据mapper调用查询方法
		List<TbItem> list = mapper.selectByExample(example);
		//4.获取到分页信息
		PageInfo<TbItem> pInfo = new PageInfo<>(list);
		//分装到EasyUIDataGridResult
		EasyUIDataGridResult eResult = new EasyUIDataGridResult();
		eResult.setTotal((int) pInfo.getTotal());
		eResult.setRows(pInfo.getList());
		return eResult;
	}

	@Override
	public TaotaoResult savfeItem(TbItem item, String desc) {
		
		// 1、生成商品id
		long itemId = IDUtils.genItemId();
		// 2、补全TbItem对象的属性
		item.setId(itemId);
		/*//商品状态，1-正常，2-下架，3-删除
		item.setStatus((byte) 1);
		Date date = new Date();
		item.setCreated(date);
		item.setUpdated(date);
		// 3、向商品表插入数据
		mapper.insert(item);
		// 4、创建一个TbItemDesc对象
		TbItemDesc itemDesc = new TbItemDesc();
		// 5、补全TbItemDesc的属性
		itemDesc.setItemId(itemId);
		itemDesc.setItemDesc(desc);
		itemDesc.setCreated(date);
		itemDesc.setUpdated(date);
		// 6、向商品描述表插入数据
		itemDescMapper.insert(itemDesc);*/
		
		insertItem(item, desc);
		jmsTemplate.send(destination,new MessageCreator() {
			
			@Override
			public Message createMessage(Session session) throws JMSException {
				// TODO Auto-generated method stub
				return session.createTextMessage(itemId + "");
			}
		});
		
		
		// 7、TaotaoResult.ok()
		return TaotaoResult.ok();
	}


	@Override
	public TaotaoResult deleteItemById(List<String> ids) {
		// 1、拿到要删除商品数组id
		/*TbItemExample tbItemExample = new TbItemExample();
		Criteria criteria = tbItemExample.createCriteria();
		criteria.a
		List<Long> idS = JsonUtils.jsonToList(ids, Long.class);*/
		for(String id1 :ids) {
			long id = Long.parseLong(id1);
			mapper.deleteByPrimaryKey(id);
		}
		System.out.println(ids);
		
		// 2、遍历id，依次删除
		
		//商品状态，1-正常，2-下架，3-删除
		



		// 3、修改商品状态


		// 7、TaotaoResult.ok()
		return TaotaoResult.ok();
	}

	@Override
	public TaotaoResult selectItemDescByid(Long id) {
		//1.注入mapper
		//2.设置查询条件
		TbItemDesc tbItemDesc = new TbItemDesc();
		//3.开始查询
		tbItemDesc = itemDescMapper.selectByPrimaryKey(id);
		//4.提交数据
		return TaotaoResult.ok(tbItemDesc);
	}

	@Override
	public TaotaoResult SendMessage(Long itemId) {
		// TODO Auto-generated method stub
		//添加发s送消息的业务逻辑
		jmsTemplate.send(destination,new MessageCreator() {
			
			@Override
			public Message createMessage(Session session) throws JMSException {
				// TODO Auto-generated method stub
				return session.createTextMessage(itemId + "");
			}
		});
		return TaotaoResult.ok();
	}

	@Override
	/**
	 * 根据商品id查询商品
	 */
	public TbItem getItemById(Long itemId) {
		
		//添加缓存
		
		//1.从缓存中获取数据，如果有直接返回
		try {
			String jsonStr = client.get(ITEM_INFO_KEY + ":" + itemId + ":BASE");
			if(StringUtils.isNotBlank(jsonStr)) {
				client.expire(ITEM_INFO_KEY+":"+itemId+":BASE", ITEM_INFO_KEY_EXPIRE);
				return JsonUtils.jsonToPojo(jsonStr, TbItem.class);
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		// 注入Mapper
		// 调用方法
		TbItem tbItem  = mapper.selectByPrimaryKey(itemId);
		
		//添加缓存到redis数据库中
			//注入jedisclient
		try {
			client.set(ITEM_INFO_KEY+":"+itemId+":BASE",JsonUtils.objectToJson(tbItem));
			//设置缓存的有效期
			client.expire(ITEM_INFO_KEY+":"+itemId+":BASE", ITEM_INFO_KEY_EXPIRE);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
		return tbItem;
	}


	@Override
	public TbItemDesc geTbItemDescByItemId(Long itemId) {
		// TODO Auto-generated method stub
		
		//添加缓存
		
				//1.从缓存中获取数据，如果有直接返回
			try {
				String jsonStr = client.get(ITEM_INFO_KEY + ":" + itemId + ":DESC");
				if(StringUtils.isNotBlank(jsonStr)) {
					System.out.println("已有缓存");
					client.expire(ITEM_INFO_KEY+":"+itemId+":DESC", ITEM_INFO_KEY_EXPIRE);
					return JsonUtils.jsonToPojo(jsonStr, TbItemDesc.class);
				}
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		
		TbItemDesc tbItemDesc = itemDescMapper.selectByPrimaryKey(itemId);
		
		//添加缓存到redis数据库中
		//注入jedisclient
		try {
			client.set(ITEM_INFO_KEY+":"+itemId+":DESC",JsonUtils.objectToJson(tbItemDesc));
			//设置缓存的有效期
			client.expire(ITEM_INFO_KEY+":"+itemId+":DESC", ITEM_INFO_KEY_EXPIRE);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return tbItemDesc;
	}

	@Override
	public TaotaoResult insertItem(TbItem item, String desc) {
		// 1、生成商品id
		//long itemId = IDUtils.genItemId();
		// 2、补全TbItem对象的属性
		//item.setId(itemId);
		//商品状态，1-正常，2-下架，3-删除
		item.setStatus((byte) 1);
		Date date = new Date();
		item.setCreated(date);
		item.setUpdated(date);
		// 3、向商品表插入数据
		mapper.insert(item);
		// 4、创建一个TbItemDesc对象
		TbItemDesc itemDesc = new TbItemDesc();
		// 5、补全TbItemDesc的属性
		itemDesc.setItemId(item.getId());
		itemDesc.setItemDesc(desc);
		itemDesc.setCreated(date);
		itemDesc.setUpdated(date);
		// 6、向商品描述表插入数据
		itemDescMapper.insert(itemDesc);
		
		return TaotaoResult.ok();
	}


}
